package com.goldsprite.gdxcore.entities;
import com.badlogic.gdx.graphics.*;
import com.goldsprite.gdxcore.screens.*;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.goldsprite.utils.math.Vector2;

public class GameWorldGroup extends WidgetGroup{
	private Vector2 tmpCoord = new Vector2();
	public OrthographicCamera screenCam;
	public GScreen screen;
	
	
	public GameWorldGroup(){
	}
	public void setScreenCam(Camera screenCam){
		this.screenCam = (OrthographicCamera) screenCam;
	}
	public void setScreen(GScreen screen){
		this.screen = screen;
	}
	public IGScreen getScreen(){
		return this.screen;
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		updateVirtualViewport();
		////绘制背景
		//batch.draw(blackBackground, getX(), getY(), getWidth()*getScaleX(), getHeight()*getScaleY());
		super.draw(batch, parentAlpha);
	}

	public com.badlogic.gdx.math.Vector2 stageStartCoord = new com.badlogic.gdx.math.Vector2();
	//有背景会不准
	private void updateVirtualViewport(){
		Group layout = getParent();
		float sx = 0, sy = 0, lw = layout.getWidth(), lh = layout.getHeight();
		float screenWidth = screenCam.viewportWidth;
		float screenHeight = screenCam.viewportHeight;
		float widthRatio = screenWidth / lw;
		float heightRatio = screenHeight / lh;
		float alignScl = 1 / heightRatio;//高对齐
		float viewWidth = screenWidth * alignScl;
		float viewHeight = screenHeight * alignScl;
		float x = sx + lw/2 - viewWidth/2;
		float y = sy;
		if(lw < viewWidth){
			alignScl = 1 / widthRatio;//宽对齐
			viewWidth = screenWidth * alignScl;
			viewHeight = screenHeight * alignScl;
			x = sx;
			y = sy + lh/2 - viewHeight/2;
		}
		//LogViewerService.log("获取窗口宽: %s, 变换后宽: %s, 窗口高: %s, 变换后高: %s, ", getWidth(), viewWidth, getHeight(), viewHeight);
		setSize(screenWidth, screenHeight);
		setScale(alignScl);
		setPosition(x, y);
		localToStageCoordinates(stageStartCoord.set(0, 0));
		//LogViewerService.log("更新虚拟视口: \nx: %.1f, y: %.1f, \nstageX: %.1f, stageY: %.1f, \nsclX: %.2f, sclY: %.2f, \nsizeX: %.1f, sizeY: %.1f, \nrealSizeX: %.1f, realSizeY: %.1f", x, y, stageStartCoord.x, stageStartCoord.y, getScaleX(), getScaleY(), getWidth(), getHeight(), getWidth()*getScaleX(), getHeight()*getScaleY());
	}

}
